﻿namespace Bootsy.Tests.Core

import System
import NUnit.Framework
import Bootsy.PEG

[TestFixture]
class PrimitivesTestFixture:
	
	[Test]
	def TestMatches():
		language = literal("!jira") >> (literal("top") | literal("search")) >> eof()
		
		assert matches(language, "!jira top")
		assert matches(language, "   !jira search   ")
		assert not matches(language, "!jira top search")
		assert not matches(language, "")
		assert not matches(language, "!jira")
		
	[Test]
	def TestParse():

		thePrefix = ""
		theNumber = 0
		matchPrefix = do (value as string):
			thePrefix = value
		matchNumber = do (value as int):
			theNumber = value
		
		language = literal("!jira") >> re(/^\w+-/, matchPrefix) >> integer(matchNumber) >> eof()
		parse(language, "!jira COR-42")()
		Assert.AreEqual("COR-", thePrefix)
		Assert.AreEqual(42, theNumber)
		
	[Test]
	def TestLiteralHandler():
		rules = []
		foo = literal("foo") do (value):
			rules.Add(value)
			
		bar = literal("bar") do (value):
			rules.Add(value)
			
		language = (foo | bar) >> eof()
		parse(language, "foo")()
		Assert.AreEqual(["foo"], rules)
		parse(language, "bar")()
		Assert.AreEqual(["foo", "bar"], rules)
		
		
	[Test]
	def TestRE():
		
		language = re(/^\w+-/) >> eof()
		assert matches(language, "foo-")
		assert matches(language, "     foo-   ")
		assert not matches(language, "bar foo-   ")
		
		language = literal("!jira") >> re(/^\w+-/) >> integer() >> eof()
		assert matches(language, "!jira AAA-333 ")
		assert not matches(language, "!jira 333-AAA")
		assert not matches(language, "!jira 333-AAA 333")
		
